package com.dtf.admin.tag;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.dtf.admin.staff.vo.SystemStaff;
import com.dtf.core.common.utils.Const;
import com.dtf.core.common.utils.Contanst;

public class LeftMenuTag extends TagSupport{
	
	
	
	@Override
	public int doEndTag() throws JspException {
		show();
		return super.doEndTag();
	}

	@Override
	public int doStartTag() throws JspException {
		// TODO Auto-generated method stub
		return super.doStartTag();
	}
	
	@Override
	public void release(){
	      super.release();
	}

	public void show(){
		HttpSession session =  super.pageContext.getSession();
		SystemStaff staff = (SystemStaff) session.getAttribute(Contanst.CURRENT_USER_KEY);
		
		String htm = "";
		if (staff == null) {
			htm = "<script>alert('请先登录');window.location.href='/admin/login.jsp';</script>";
		}else{
			List<Map<String,Object>> menus = (List<Map<String, Object>>) session.getAttribute(Contanst.CURRENT_MENU_KEY);
			
			ServletRequest request = super.pageContext.getRequest();
			String menu_path = request.getAttribute(Contanst.CURRENT_MENU_NOW_PATH)+"";
			String[] paths = menu_path.split("\\.");
			
			htm = objToHtml(menus, "-1",paths);
//			System.out.println(htm);
		}
		
		JspWriter out = pageContext.getOut();
		try {
			out.print(htm);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String objToHtml(List<Map<String,Object>> menus,String pid,String[] paths){
		
		
		String html = "";
		if ("-1".equals(pid)) {
			html += "<ul class='nav nav-list'>";
		}else{
			html += "<ul class='submenu nav-show'>";
		}
		int count = 0;
		for (Map menu : menus) {
			String parent_id = Const.getStrValue(menu, "parent_id");
			if (!parent_id.equals(pid)) {
				continue;
			}
			count ++;
			String menu_id = Const.getStrValue(menu, "menu_id");
			String menu_name = Const.getStrValue(menu, "menu_name");
			String menu_url = Const.getStrValue(menu, "menu_url");
			String class_style = Const.getStrValue(menu, "class_style");
			int level = Const.getIntValue(menu, "level");
			String liClass = "";
			for (int i = 0; i < paths.length; i++) {
				if (paths[i].equals(menu_id)) {
					liClass = "active open";
					if (i == paths.length-1) {
						liClass = "active";
					}
					break;
				}
			}
			
			if (!"-1".equals(pid) && Const.strIsEmpty(class_style) && level < 3) {
				class_style = "fa-caret-right";
			}else if("-1".equals(pid) && Const.strIsEmpty(class_style)){
				class_style = "fa-bars";
			}
			String basePath = menu_url;
			String chtm = objToHtml(menus, menu_id,paths);
			String hasC = "".equals(chtm)? "" : "class='dropdown-toggle' ";
			String chtm2 = "".equals(chtm)? "" : "<b class='arrow fa fa-angle-down'></b>";
			html += "<li class='"+liClass+"' >"+
							"<a href='"+basePath+"' "+hasC+" >"+
								"<i class=\"menu-icon fa "+class_style+"\"></i>" +
								"<span class='menu-text'>"+menu_name+"</span>"+
								chtm2 +
							"</a>"+
							"<b class='arrow'></b>";
			html += chtm;
			html += "</li>";
		}
		html += "</ul>";
		if (count == 0) {
			html = "";
		}
		return html;
	}
}
